import 'package:flutter/material.dart';
import 'fravoritesAlert.dart';
import 'package:hairen_app/server/server.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hairen_app/server/state/UserInfoModel.dart';
class Intro extends StatefulWidget {
  final Map moveInto;
  Intro(this.moveInto);
  @override
  State<StatefulWidget> createState() {
    return new _Intro();
  }
}


class _Intro extends State<Intro> {
  bool isfav=false; //是否搜藏
  Map favinfo;//搜藏信息
  Map moveInto;
  @override
  void initState() {
    super.initState();
    moveInto=widget.moveInto;
    isFavorite();
  }

  @override
  void dispose() {
    super.dispose();


  }
  Future isFavorite() async {
    var data= await Server.isFavorite(data: {'mid':widget.moveInto['id'].toString()});
    if(data['code']==1){
      isfav=data['msg'];
      favinfo=data['data'];
    }
  }
  //选择收藏夹
  Future addFavorites() async{
    if(isfav){// 取消搜藏
      Server.cancelFavorite(data: {'mid':widget.moveInto['id'].toString(),'fid':favinfo['fid']}).then((ret){
         if(ret['code']==1){
           Fluttertoast.showToast(
             msg: ret['msg'],
             gravity: ToastGravity.CENTER,
             toastLength: Toast.LENGTH_SHORT,
             timeInSecForIos: 1,
           );
           isFavorite();
         }else{
           Fluttertoast.showToast(
             msg: ret['msg'],
             gravity: ToastGravity.CENTER,
             toastLength: Toast.LENGTH_SHORT,
             timeInSecForIos: 1,
             bgcolor: "#f56464",
           );
         }
      });
    }else{
      return  await showDialog<Null>(
        context: context,
        barrierDismissible: true, // user must tap button!
        builder: (BuildContext context) {
          return new  FravoritesAlert(int.parse(widget.moveInto['id']),updatCallback: isFavorite,);
        },
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return new ListView(
      padding: EdgeInsets.only(top: 0.0),
      children: <Widget>[
        new Column(
          children: <Widget>[
            new SizedBox(
              height: 20.0,
            ),
            new ListTile(
              leading: new Image.network(
                "${moveInto['logo']}",
                width: 100.0,
                fit: BoxFit.fill,
              ),
              title: new Column(
                children: <Widget>[
                  new Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      new Expanded(
                        flex: 2,
                        child: new Text(
                          "${moveInto['title']} ${moveInto['title_type']}",
                          textAlign: TextAlign.left,
                          style: new TextStyle(fontSize: 14.0),
                        ),
                      ),
                      new Expanded(
                        flex: 1,
                        child:new Container(
                        alignment: Alignment.centerRight,
                        child: new InkWell(
                            onTap: () {
                              if(UserInfoModel.isLogin){
                                addFavorites();
                              }else{
                                Fluttertoast.showToast(
                                  msg: "请登陆后在继续",
                                  gravity: ToastGravity.CENTER,
                                  toastLength: Toast.LENGTH_SHORT,
                                  timeInSecForIos: 1,
                                  bgcolor: "#f56464",
                                );
                              }
                            },
                            child: new Container(
                              width: 60.0,
                              height: 25.0,
                              padding: EdgeInsets.only(top: 3.0, bottom: 3.0),
                              decoration: new BoxDecoration(
                                  color:isfav?Theme.of(context).backgroundColor:Colors.black12,
                                  borderRadius: BorderRadius
                                      .all(new Radius.circular(10.0))),
                              child: new Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: <Widget>[
                                  new Icon(
                                    Icons.favorite_border,
                                    size: 10.0,
                                    color: Colors.white,
                                  ),
                                  new SizedBox(width: 5.0),
                                  new Text('收藏',
                                      style: new TextStyle(
                                          fontSize: 10.0,
                                          color: Colors.white))
                                ],
                              ),
                            )),
                      ),
                      )
                    ],
                  ),
                  new SizedBox(
                    height: 5.0,
                  ),
                  new Row(
                    children: <Widget>[
                      new Icon(Icons.ondemand_video,
                          size: 12.0,
                          color: Color.fromRGBO(156, 156, 156, 1.0)),
                      new SizedBox(
                        width: 5.0,
                      ),
                      new Text('${moveInto['click']}',
                          style: new TextStyle(
                              fontSize: 10.0,
                              color: Color.fromRGBO(156, 156, 156, 1.0))),
                      new SizedBox(
                        width: 10.0,
                      ),
                      new Icon(Icons.favorite_border,
                          size: 12.0,
                          color: Color.fromRGBO(156, 156, 156, 1.0)),
                      new SizedBox(
                        width: 5.0,
                      ),
                      new Text('0人',
                          style: new TextStyle(
                              fontSize: 10.0,
                              color: Color.fromRGBO(156, 156, 156, 1.0))),
                    ],
                  ),
                  new SizedBox(
                    height: 5.0,
                  ),
                ],
              ),
              subtitle: new Container(
                height: 90.0,
//                color: Colors.black,
                child: new Text(
                    moveInto['intro'] == null
                        ? "暂无简介"
                        : moveInto['intro'].toString(),
                    maxLines: 5,
                    style: new TextStyle(
                        fontSize: 13.0,
                        color: Color.fromRGBO(156, 156, 156, 1.0))),
              ),
            ),
            new SizedBox(
              height: 20.0,
            ),
            new Divider(),
            new SizedBox(
              height: 20.0,
            ),
            new Padding(
              padding: EdgeInsets.only(left: 10.0, right: 10.0),
              child: new Column(
                children: <Widget>[
                  new Row(
                    children: <Widget>[
                      new Expanded(
                        flex: 1,
                        child: new Text(
                          '导演：${moveInto['director']}',
                          style: new TextStyle(fontSize: 13.0),
                        ),
                      ),
                      new Expanded(
                        flex: 1,
                        child: new Text('主演：${moveInto['actor']}',
                            style: new TextStyle(fontSize: 13.0)),
                      ),
                    ],
                  ),
                  new SizedBox(
                    height: 10.0,
                  ),
                  new Row(
                    children: <Widget>[
                      new Expanded(
                        flex: 1,
                        child: new Text(
                            '类型：${moveInto['type']} ${moveInto['sutype']}',
                            style: new TextStyle(fontSize: 13.0)),
                      ),
                      new Expanded(
                        flex: 1,
                        child: new Text('地区：${moveInto['area']}',
                            style: new TextStyle(fontSize: 13.0)),
                      ),
                    ],
                  ),
                  new SizedBox(
                    height: 10.0,
                  ),
                  new Row(
                    children: <Widget>[
                      new Expanded(
                        flex: 1,
                        child: new Text('上映：${moveInto['show']}',
                            style: new TextStyle(fontSize: 13.0)),
                      ),
                      new Expanded(
                        flex: 1,
                        child: new Text('更新：${moveInto['update']} ',
                            style: new TextStyle(fontSize: 13.0)),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            new SizedBox(
              height: 20.0,
            ),
            new Divider(),
            new Padding(
              padding: EdgeInsets.only(left: 10.0, right: 10.0),
              child: new Column(
                children: <Widget>[
                  new Container(
                    alignment: Alignment.centerLeft,
                    child: new Text('简介：',style:new TextStyle(fontSize: 16.0),),
                    padding: EdgeInsets.only(bottom: 10.0),
                  ),
                  new Container(
//                      height: 90.0,
//                color: Colors.black,
                    padding: EdgeInsets.only(bottom: 20.0),
                    child: new Text(
                        moveInto['intro'] == null
                            ? "暂无简介"
                            : "        ${moveInto['intro'].toString()}",
                        style: new TextStyle(
                            fontSize: 14.0,
                            color: Color.fromRGBO(156, 156, 156, 1.0))),
                  ),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }

}

